import config from './chartConfig';
import { cloneDeep, assign } from 'lodash';
import { moment } from 'istrong-timeutil';
import Highcharts from 'istrong-highcharts';

let mergeOptions = (list, warn_val, ensure_val, beginTime, endTime) => {
  let chartOpt = cloneDeep(config);

  let waterData = [];

  let minWaterVal, maxWaterVal;
  if (warn_val !== undefined) {
    maxWaterVal = minWaterVal = warn_val;
  }
  if (ensure_val !== undefined) {
    minWaterVal = minWaterVal !== undefined ? Math.min(ensure_val, minWaterVal) : ensure_val;
    maxWaterVal = maxWaterVal !== undefined ? Math.max(ensure_val, maxWaterVal) : ensure_val;
  }

  minWaterVal = minWaterVal === undefined ? (list.length > 0 ? list[0].val : 0) : minWaterVal;
  maxWaterVal = maxWaterVal === undefined ? (list.length > 0 ? list[0].val : 100) : maxWaterVal;
  let minTime = list.length === 0 ? parseInt(moment(beginTime).format('x'), 10) : parseInt(moment(list[0].time).format('x'), 10);
  let maxTime = list.length === 0 ? parseInt(moment(moment(endTime).add('hour', 1).format('YYYY-MM-DDTHH:00:00')).format('x'), 10) :
    parseInt(moment(list[list.length - 1].time).format('x'), 10);

  list.forEach((item, i) => {
    waterData.push({
      x: parseInt(moment(item.time).format('x'), 10),
      y: item.val
    });
    minWaterVal = Math.min(minWaterVal || 0, item.val);
    maxWaterVal = Math.max(maxWaterVal || 0, item.val);
  });
  minWaterVal = minWaterVal || 0;
  maxWaterVal = maxWaterVal || 300;
  var yInterval = parseFloat((((maxWaterVal + 1) - (minWaterVal)) / 6).toFixed(2));

  chartOpt = assign(chartOpt, {
    xAxis: {
      labels: {
        formatter: function () {
          return moment(this.value).format('HH时<br/>DD日')
        },
        autoRotation: 0,
        padding: 30,
        style: {
          textOverflow: 'none'
        }
      },
      lineColor: '#808080',
      lineWidth: 0,
      tickColor: '#808080',
      tickWidth: 1,
      tickLength: 5,
      tickPosition: 'outside',
      gridLineWidth: 1,
      endOnTick: true,
      startOnTick: true,
      tickAmount: 6,
      min: minTime,
      max: maxTime,
      tickInterval: 1000 * 3600
    },
    yAxis: [
      {
        lineWidth: 1,
        lineColor: '#808080',
        gridLineDashStyle: 'longdash',
        gridLineColor: '#E0E0E0',
        tickInterval: yInterval,
        endOnTick: false,
        labels: {
          align: 'right',
          x: -5,
          y: 0,
          formatter: function () {
            return this.value.toFixed(2)
          }
        },
        title: {
          text: 'm',
          align: 'high',
          rotation: 0,
          x: 50,
          y: -8
        },
        max: maxWaterVal + 1,
        min: minWaterVal >= 0 ? Math.max(minWaterVal - 1, 0) : minWaterVal,
        plotLines: []
      }
    ],
    series: [{
      data: waterData,
      color: '#5399d7'
    }]
  });
  if (ensure_val !== undefined) {
    chartOpt.yAxis[0].plotLines.push({
      value: ensure_val,
      color: '#fc575b',
      width: 1,
      id: 'ensure',
      zIndex: 5
    });
  }

  if (warn_val !== undefined) {
    chartOpt.yAxis[0].plotLines.push({
      value: warn_val,
      color: '#ed9f4c',
      width: 1,
      id: 'warn',
      zIndex: 4
    });
  }

  return chartOpt;
}
let renderChart = (chartViewer, chartId, waterData, beginTime, endTime, chartOptInitedAfter) => {
  if (chartViewer) {
    chartViewer.destroy();
  }
  let list = waterData.data,
    warn_val = waterData.flood_val,
    ensure_val = waterData.ensure_val;

  let chartOpt = mergeOptions(list, warn_val, ensure_val, beginTime, endTime);
  if (chartOptInitedAfter) {
    chartOptInitedAfter(chartOpt);
  }
  chartOpt.chart.renderTo = chartId;
  chartViewer = new Highcharts.Chart(chartOpt);
  return chartViewer;
};

export default renderChart;
